//
//  HomePageCommentView.m
//  ShowYork
//
//  Created by hxh on 2020/11/19.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "CampusQuestionReplyView.h"

typedef void(^doneBlock)(NSString *content);

@interface CampusQuestionReplyView ()<UITextViewDelegate>


@property (nonatomic,strong)UIView *inputView;
@property (nonatomic,strong)UILabel *inputPlaceLb;
@property (nonatomic,strong)UIView *inputContainView;
@property (nonatomic,strong)UILabel *inputSendLb;
@property (nonatomic,strong)UIButton *inputSendBtn;
@property (nonatomic,strong)UITextView *inputTextView;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,copy)doneBlock done;

@end

@implementation CampusQuestionReplyView

- (instancetype)initWithCompleteBlock:(void (^)(NSString * content))completeBlock{
    if (self = [super init]) {
        [self setUI];
        [self registNote];
        if (completeBlock) {
            self.done = ^(NSString *content) {
                completeBlock(content);
            };
        }
    }
    return self;
}


- (void)setUI{
    
    self.frame = CGRectMake(0,0, kScreenW,kScreenH);
    
    self.coverBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,kScreenW, kScreenH)];
    self.coverBtn.backgroundColor = RGBA(0, 0, 0, 0.5);
    [self.coverBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.coverBtn];

    
    self.inputView = [[UIView alloc]init];
    self.inputView.frame = CGRectMake(0,kScreenH, kScreenW, kScreenW * 0.36);
    self.inputView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.inputView];
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(110);
    }];
    
    
    self.inputContainView = [[UIView alloc]init];
    self.inputContainView.backgroundColor = RGBA(240, 240, 240, 1);
    self.inputContainView.layer.cornerRadius = 10;
    self.inputContainView.layer.masksToBounds = YES;
    [self.inputView addSubview:self.inputContainView];
    [self.inputContainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputView).offset(12);
        make.left.equalTo(self.inputView).offset(16);
        make.right.equalTo(self.inputView).offset(-60);
        make.bottom.mas_equalTo(self.inputView).offset(-12);
    }];
    
    self.inputPlaceLb = [[UILabel alloc]init];
    self.inputPlaceLb.text = @"快来发布你的评论…";
    self.inputPlaceLb.textColor = C153;
    self.inputPlaceLb.font = font(14);
    [self.inputView addSubview:self.inputPlaceLb];
    [self.inputPlaceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.inputView).offset(28);
        make.top.equalTo(self.inputView).offset(19);
    }];
    
    self.inputSendLb = [[UILabel alloc]init];
    self.inputSendLb.text = @"发送";
    self.inputSendLb.textColor = RGBA(163, 163, 163, 163);
    self.inputSendLb.font = [UIFont boldSystemFontOfSize:14];
    self.inputSendLb.textAlignment = NSTextAlignmentCenter;
    [self.inputView addSubview:self.inputSendLb];
    [self.inputSendLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.inputContainView.mas_right);
        make.right.equalTo(self.inputView);
        make.top.equalTo(self.inputPlaceLb);
    }];
    
    
    self.inputSendBtn = [[UIButton alloc]init];
    [self.inputSendBtn addTarget:self action:@selector(sendMethod) forControlEvents:UIControlEventTouchUpInside];
    self.inputSendBtn.userInteractionEnabled = NO;
    [self.inputView addSubview:self.inputSendBtn];
    
    [self.inputSendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.inputContainView.mas_right);
        make.right.equalTo(self.inputView);
        make.top.equalTo(self.inputContainView);
        make.height.mas_equalTo(40);
    }];
    
    self.inputTextView = [[UITextView alloc]init];
    self.inputTextView.backgroundColor = [UIColor clearColor];
    self.inputTextView.delegate = self;
    self.inputTextView.textColor = RGBA(51, 51, 51, 1);
    self.inputTextView.font = font(14);
    
    [self.inputView addSubview:self.inputTextView];
    [self.inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.inputContainView);
        make.left.equalTo(self.inputContainView).offset(8);
        make.right.equalTo(self.inputContainView).offset(-8);
    }];
    
}
- (void)coverMethod{
    self.done(@"");
    [self dismiss];
}

- (void)sendMethod{
    self.done(self.inputTextView.text);
    [self dismiss];
}

- (void)dismiss{
    
    [self.inputView endEditing:YES];

    [self.inputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(self.inputView.cl_height);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [self removeFromSuperview];
        }
    }];
}

- (void)registNote{
    //监听键盘frame改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
//键盘即将弹出
- (void)keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度 keyboardHeight
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
   
    [self.inputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-keyboardRect.size.height);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}


- (void)beginTF{
     [self showInputTextView];
}

- (void)showInputTextView{
    [self.inputTextView becomeFirstResponder];
}

- (void)show{
    
    [self layoutIfNeeded];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self.inputTextView becomeFirstResponder];

}

- (void)textViewDidChange:(UITextView *)textView{
    
    self.inputPlaceLb.hidden = textView.text.length > 0;
    
    NSString *toBeString = textView.text;
 
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 200){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:200];
            if (rangeIndex.length == 1){
                textView.text = [toBeString substringToIndex:200];
                if (toBeString.length > 200) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,200)];
                textView.text = [toBeString substringWithRange:rangeRange];
           
                if (rangeRange.length < toBeString.length) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
            }
        }
    }
    
    if ([textView.text cl_removeSpaceString].length > 0) {
        self.inputSendBtn.userInteractionEnabled = YES;
        self.inputSendLb.textColor = [RPAppManager themColor];
    }else{
        self.inputSendBtn.userInteractionEnabled = NO;
        self.inputSendLb.textColor = RGBA(163, 163, 163, 1);
    }
    
}

#pragma mark - 新增阴影效果
- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
    theView.layer.shadowColor = theColor.CGColor;
    theView.layer.shadowOffset = CGSizeMake(0,0);
    theView.layer.shadowOpacity = 0.1;
    theView.layer.shadowRadius = 1;
// 单边阴影 顶边
    float shadowPathWidth = theView.layer.shadowRadius;
    CGRect shadowRect = CGRectMake(0, 0, theView.bounds.size.width, shadowPathWidth);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
    theView.layer.shadowPath = path.CGPath;
}



@end
